//
//  PictureViewController.m
//  Interface
//
//  Created by Letme on 15-4-16.
//  Copyright (c) 2015年 zhenjiea. All rights reserved.
//

#import "PictureViewController.h"
#import "MJRefresh.h"
@interface PictureViewController ()

@end

@implementation PictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    

#warning 有值
    
    self.picArray = [NSMutableArray array];
    self.photoInfo = [NSMutableArray array];
    self.trip_nameArray = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    //flowlayout.itemSize = CGSizeMake(115, 115);
    flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 20) / 3, (self.view.frame.size.width - 10) / 3);
    flowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //flowlayout.minimumInteritemSpacing = 1;
    flowlayout.minimumLineSpacing = 10;
#warning 设置头
    //flowlayout.headerReferenceSize = CGSizeMake(0, 50);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) collectionViewLayout:flowlayout];
    
    [self.view addSubview:self.myCollectionView];
    [self.myCollectionView release];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
#warning 设置
    [self.myCollectionView registerClass:[PicCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:self.myCollectionView];
    
    
    
    self.navigationItem.title = [NSString stringWithFormat:@"%@图片", self.name];
    

    self.isUpLoading = NO;
    NSLog(@"%s", __func__);
#warning
    [self addHeader];
    [self addFooter];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ScrollViewController *scrVC = [[ScrollViewController alloc] init];
    scrVC.allPicUrlArray = self.picArray;
    scrVC.numberOfGesture = indexPath.row;
    scrVC.urlForAFN = self.url_str;
    scrVC.tripNameArray = self.trip_nameArray;
    scrVC.photoInfoArray = self.photoInfo;
    [self.navigationController pushViewController:scrVC animated:YES];
}


- (void)addHeader
{
    
    __block PictureViewController *picVC = self;
#warning
    [self.myCollectionView addHeaderWithCallback:^{
    NSLog(@"%s", __func__);
        picVC.isUpLoading = NO;
        [picVC GetPictures:0];
    }];
    [self.myCollectionView headerBeginRefreshing];
}
- (void)addFooter
{
    __block PictureViewController *picVC = self;
    [self.myCollectionView addFooterWithCallback:^{
        NSLog(@"%s", __func__);

        picVC.isUpLoading = YES;
        [picVC GetPictures:picVC.nextCursor];
    }];
}


- (void)GetPictures:(NSInteger)start
{
    NSLog(@"%s", __func__);
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/5/%@/photos/?start=%ld&count=21&gallery_mode=true", self.url_str, start];
#warning
    NSLog(@".........%@", self.url_str);
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        
        NSMutableDictionary *dic = responseObject;

        self.nextCursor = [[dic objectForKey:@"next_start"] integerValue];
        NSLog(@"nextcursor %ld ", self.nextCursor);
        if (self.isUpLoading == NO) {
            [self.picArray removeAllObjects];
        }
        
        
        NSMutableArray *array = [dic objectForKey:@"items"];
        for (NSDictionary *smallDic in array) {
            NSString *image_url = [smallDic objectForKey:@"photo"];
            [self.picArray addObject:image_url];
            NSDictionary *dic = [smallDic objectForKey:@"photo_info"];
            [self.photoInfo addObject:dic];
            //NSLog(@"%ld", self.picArray.count);
            NSString *headname = [smallDic objectForKey:@"trip_name"];
            [self.trip_nameArray addObject:headname];
        }
        
        
        [self.myCollectionView footerEndRefreshing];
        [self.myCollectionView headerEndRefreshing];
        [self.myCollectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor grayColor];
    NSString *str = [self.picArray objectAtIndex:indexPath.row];
    //NSLog(@"aaaaaaaaaaaaaaaaaaaaa%@", str);
    cell.str = str;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
